package com.DeliveryProgram.www.service;

import com.DeliveryProgram.www.constant.UScreen;
import com.DeliveryProgram.www.dao.RecationChange;
import com.DeliveryProgram.www.dao.UserLoad;
import com.DeliveryProgram.www.dao.UserSave;
import com.DeliveryProgram.www.po.*;
import com.DeliveryProgram.www.util.Scanners;
import com.DeliveryProgram.www.util.Screens;

import java.util.List;

/**处理用户与商户的交互*/
public class Recation {

    /**用户获取折扣*/
    public static void getCoupons(User us, Business bu){
        /**用户获取折扣*/
        us.setCoupons(new Coupons(bu.getCoupons().get(0).getId(),bu.getCoupons().get(0).getDiscount(),bu.getShopname()));
        /**商家剩余折扣减少*/
        bu.getCoupons().get(0).getOut();
        RecationChange.addHasCoupons(us);
        RecationChange.updateMount(bu);
    }

    /**用户对外卖结算*/
    public static void makeOrder(List<Menu> list, Business bu, User us){
        while(true){
            /**查看菜单*/
            for(int i=0;i<list.size();i++){
                System.out.println(list.get(i).getName()+list.get(i).getMount()+"份");
            }
           System.out.println("是否支付");
            System.out.println("1-是 | 2-否");
            switch (Scanners.getChoice()) {
                /**输入0离开订单查询*/
                case 1:{
                    Order od=new Order();
                    od.setOdmu(list);
                    addr(od,us);
                    if(od.getAddress()==null){
                        break;
                    }
                    pay(list,us,bu,od);
                    System.out.println("下单完成");
                    Scanners.pause();
                    return;
                }
                case 2: {
                    return;
                }
                default: {
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**地址*/
    public static void addr(Order od,User us){
        Screens.clear();
        while (true) {
            UScreen.checkAddress(us);
            System.out.println("0-退出 | 1-添加新的地址 | 2-使用已有地址");
            switch (Scanners.getChoice()) {
                case 0:return;
                case 1: {
                    System.out.println("请输入新的地址");
                    String addr = Scanners.getScanner();
                    System.out.println("请输入电话");
                    String ph = Scanners.getScanner();
                    us.getAddresses().add(new Address(addr, ph));
                    UserSave.addAddress(us);
                    UserLoad.getAddrNewId(us);
                    break;
                }
                case 2: {
                    Screens.clear();
                    choiceAddr(us,od);
                    if(od.getAddress()!=null){
                        return;
                    }
                    break;
                }
                default: {
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**获取订单地址*/
    public static void choiceAddr(User us,Order od) {
        while (true) {
            int choice = 0;
            /**防止用户输入不存在的订单*/
            do {
                UScreen.checkAddress(us);
                System.out.println("请输入需要修改的地址编号 | 0-返回");
                choice = Scanners.getChoice();
                if (choice - 2 > us.getAddresses().size()) {
                    System.out.println("请输入存在的订单");
                    Scanners.pause();
                    Screens.clear();
                }
            } while (choice - 2 > us.getAddresses().size());
            switch (choice) {
                /**输入0离开订单查询*/
                case 0:
                    return;
                default: {
                    /**获取本来订单编号*/
                    choice -= 1;
                   od.setAddress(us.getAddresses().get(choice));
                    return;
                }

            }
        }
    }

    /**支付*/
    public static void pay(List<Menu> list,User us,Business bu,Order od) {
        double money=0;
        for (int i = 0; i < list.size(); i++) {
            money += (list.get(i).getPrice() * list.get(i).getMount());
        }
      while(true) {
          money=useCoupons(us,bu,money);
          if (us.getMoney() < money) {
              System.out.println("余额不足，请充值");
              System.out.println("0-退出 1-充值");
              switch (Scanners.getChoice()) {
                  case 1: {
                      System.out.println("请输入添加金额");
                      us.addMoney(Scanners.getChoice());
                      UserSave.changeMoney(us);
                      break;
                  }
                  case 0:
                      return;
                  default: {
                      System.out.println("请输入正确的选项");
                      /**让用户确认*/
                      Scanners.pause();
                      Screens.clear();
                      continue;
                  }
              }
          }
          /**用户将钱预支付进订单中*/
          od.setMoney((int)money);
          us.payMoney((int)money);
          UserSave.buy(us,od,bu);
          UserSave.getOrderId(us,od,bu);

          for(int i=0;i<od.getOdmu().size();i++) {
              UserSave.pushMenu(od.getOdmu().get(i),od.getId());
          }
          us.setOrders(od);
          return;
      }
    }

    /**询问是否用折扣, 小数点后向上取整，增加费用为平台利润*/
    public static double useCoupons(User us,Business bu,double money){
        for(int i=0;i<us.getCoupons().size();i++){
            if(us.getCoupons().get(i).getName().equals(bu.getShopname())){
                while(true){
                System.out.println("是否使用折扣");
                System.out.println("1-是 | 2-否");
                switch (Scanners.getChoice()){
                    case 1:{

                        return Math.ceil(money*us.getCoupons().get(i).getDiscount());
                    }
                    case 2: {
                        return Math.ceil(money);
                    }
                    default: {
                        System.out.println("请输入正确的选项");
                        /**让用户确认*/
                        Scanners.pause();
                        Screens.clear();
                        break;
                    }
                }
                }
            }
        }
        return 0;
    }

}
